package com.jy.wms.controller;

import com.github.pagehelper.Page;
import com.joyi.gate.comm.SessionInfo;
import com.joyi.gate.safety.vo.UserInfo;
import com.jy.wms.common.SetStatus;
import com.jy.wms.common.base.BaseController;
import com.jy.wms.common.base.BaseSessionInfo;
import com.jy.wms.common.base.ReturnType;
import com.jy.wms.common.base.ServiceException;
import com.jy.wms.pojo.SysHz;
import com.jy.wms.pojo.WmsPdaShipment;
import com.jy.wms.service.WmsPdaShipmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * @ClassName: WmsPdaShipmentController
 * @Description: 出库发运  发运任务
 * @Author Yjs
 * @Date Tue Feb 06 10:49:59 GMT+08:00 2018
 */
@RestController
@RequestMapping("/wmspdashipment")
@Api(value = "/wmspdashipment", description = "出库发运--发运任务")
public class WmsPdaShipmentController extends BaseController<WmsPdaShipmentService, WmsPdaShipment> {
    /**
     * pda查看发运任务信息
     *
     * @param entity
     * @param session
     * @return
     */
    @PostMapping(value="/list")
    @ApiOperation(value = "pda发运管理 POST请求查询pda查看发运任务信息", response = JSONObject.class, notes = "POST请求查询pda查看发运任务信息")
    public @ResponseBody JSONObject getWmsPdaShipment(@RequestBody WmsPdaShipment entity, HttpSession session){
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String hzid = null;//取当前货主编号
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
                entity.setHzid(hzid);
            }
            entity.setCkid(ckid);
            Page<WmsPdaShipment> list = baseService.baseSelectPage(entity);
            json = SetStatus.setSearchType(JSONArray.fromObject(list).toString(), (int) list.getTotal());
        }catch (Exception e) {
            e.printStackTrace();
        }
        return json;
    }

    /**
     * 下发发运任务
     *
     * @param wmsPdaShipmentList
     * @param session
     * @return
     */
    @PostMapping(value = "/addPda")
    @ApiOperation(value = "pda发运管理 POST下发发运任务", response = JSONObject.class, notes = "POST下发发运任务")
    public @ResponseBody
    JSONObject addPda(@RequestBody List<WmsPdaShipment> wmsPdaShipmentList, HttpSession session) {
        JSONObject json = getJSONObject();
        try {
            UserInfo userinfo = (UserInfo) session.getAttribute(SessionInfo.USER_INFO);
            String ckid = String.valueOf(userinfo.getUser().getOrgId().get(0));//仓库编号
            String hzid = null;//取当前货主编号
            List<SysHz> hz = (List<SysHz>) session.getAttribute(BaseSessionInfo.SessionConsignor);
            if (hz.size() > 0) {
                hzid = hz.get(0).getHzid();
            }
            baseService.addPda(wmsPdaShipmentList, hzid, ckid);
            json = SetStatus.setFunctionType(ReturnType.messageType_top.getType(), ReturnType.success.toString(), "下发发运任务成功", "", JSONArray.fromObject(wmsPdaShipmentList).toString());
        } catch (ServiceException e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.warning.toString(), "下发发运任务失败", e.getMessage(), "");
        } catch (Exception e) {
            e.printStackTrace();
            json = SetStatus.setFunctionType(ReturnType.messageType_right.getType(), ReturnType.error.toString(), "下发发运任务失败", "下发发运任务失败", "");
        }
        return json;
    }

}
